package com.baseLearn.learn.classLoader;

import sun.misc.VM;

import java.util.concurrent.ConcurrentHashMap;

/**
 * 自定义一个类加载器;递归;
 */
public class MyClassLoader {
    private static ConcurrentHashMap<Object, Object> loadClassMap;

    private native void resolveClassO(Class c);

    public void loadClass(String className) {
        synchronized (getClassLoadingLock(className)) {

            //1.检查类是否被加载过
            // First, check if the class has already been loaded

        }

    }

    /**
     * 校验类名称(com.demo.Test.class)
     *
     * @param name
     */
    private Boolean checkName(String name) {
        if ((name == null) || (name.length() == 0))
            return true;
        if ((name.indexOf('/') != -1)
                || (!VM.allowArraySyntax() && (name.charAt(0) == '[')))
            return false;
        return true;
    }

    private Class<?> findLoaderClass(String className) {
        if (!checkName(className)) { //类名规范校验不通过,返回null
            return null;
        }
        findLoaderClassO();

    }

    /**
     * @param className
     * @return
     */
    private Object getClassLoadingLock(String className) {
        Object lock = this;
        if (loadClassMap != null) {
            Object newLock = new Object();
            lock = loadClassMap.putIfAbsent(className, newLock);

            if (null == lock) {  //key为空,放入了新lock
                return newLock;
            }
            return lock;
        }

        return lock;

    }
}
